#include "stdafx.h"
#include "TaskManager.h"

TaskManager::TaskManager()
{
	m_nVersion = 0;
	//loadTaskCSV();
}
TaskManager::~TaskManager()
{

}

void TaskManager::loadTaskCSV()
{
	if(m_mapTaskCSV.size() != 0)
		return;
	std::string path = singleton<GameServer>::instance().GetServerPath();
	std::string filename = path + "csv/task.csv";
#ifndef _WIN32
	filename = "./csv/task.csv";
#endif
	m_csvReader.Load(filename);

	for (Int32 i = 0; i < m_csvReader.GetRowCount(); i++)
	{
		Int32 id = m_csvReader.GetInt(i,"id");
		if(m_mapTaskCSV.find(id) != m_mapTaskCSV.end())
		{
			LOGGER_ERROR("m_mapTaskCSV.find(id) != m_mapTaskCSV.end(),id:%d",id);
			return;
		}

		m_mapTaskCSV[id].id = id;
		TaskCSVInfo& info = m_mapTaskCSV[id];
		info.type = m_csvReader.GetInt(i,"type");
		info.name = m_csvReader.GetString(i,"name");
		info.description = m_csvReader.GetString(i,"description");
		info.count_everyday = m_csvReader.GetInt(i,"count_everyday");
		info.time_limit = m_csvReader.GetInt(i,"time_limit");
		info.enable_record = m_csvReader.GetInt(i,"enable_record");
		info.enable_double = m_csvReader.GetInt(i,"enable_double");
		info.require_complete_task_id = m_csvReader.GetInt(i,"require_complete_task_id");
		info.require_level = m_csvReader.GetInt(i,"require_level");
		info.accepte_map_id = m_csvReader.GetInt(i,"accepte_map_id");
		info.accepte_npc_id = m_csvReader.GetInt(i,"accepte_npc_id");
		info.complete_map_id = m_csvReader.GetInt(i,"complete_map_id");
		info.complete_npc_id = m_csvReader.GetInt(i,"complete_npc_id");
		info.reward_wood = m_csvReader.GetInt(i,"reward_wood");
		info.reward_food = m_csvReader.GetInt(i,"reward_food");
		info.reward_exp = m_csvReader.GetInt(i,"reward_exp");
		info.reward_money = m_csvReader.GetInt(i,"reward_money");
		info.reward_items = m_csvReader.GetString(i,"reward_money");

	}
}
TaskCSVInfo* TaskManager::GetTaskCSVInfo(Int32 csv_id)
{
	std::map<Int32,TaskCSVInfo>::iterator iter = m_mapTaskCSV.find(csv_id);
	if (iter == m_mapTaskCSV.end())
	{
		LOGGER_ERROR("GetTaskCSVInfo,iter == m_mapTaskCSV.end(),csv_id:%d",csv_id);
		return NULL;
	}
	return &(iter->second);
}
void TaskManager::GetRecordFromDataServer()
{
	LOGGER_INFO("GetRecordFromDataServer");

	PacketEncoder encoder;
	encoder.AddItem(Int32(0));
	encoder.AddItem(Int32(1103));
	SendPacketToData(encoder);
}
TaskRecord* TaskManager::GetTaskRecord(Int32 task_id)
{
	std::map<Int32,TaskRecord>::iterator iter = m_mapTaskRecord.find(task_id);
	if (iter == m_mapTaskRecord.end())
	{
		return NULL;
	}
	return &(iter->second);
}
void TaskManager::updateRecord(Int32 task_id,std::string task_name,Int32 char_id0,std::string char_name0,Int32 elapsed_time,Int32 times)
{
	if (char_name0.size() >= MAX_CHAR_NAME_SIZE) return;
	Player* pPlayerDest = singleton<PlayerManager>::instance().GetByCharId(char_id0);
	if(pPlayerDest == NULL)
	{
		return;
	}
	std::map<Int32,TaskRecord>::iterator iter = m_mapTaskRecord.find(task_id);
	if (iter == m_mapTaskRecord.end())
	{
		TaskRecord& record = m_mapTaskRecord[task_id];
		record.task_id = task_id;
		record.fastest_char_id = char_id0;
		if(!char_name0.empty() && char_name0.length() < MAX_CHAR_NAME_SIZE)
			strcpy(record.fastest_char_name,char_name0.c_str());
		record.fastest_elapsed_time = elapsed_time;
		record.fastest_time = (Int32)time(NULL);
		record.fastest_break_times += 1;
		std::ostringstream os;
		pPlayerDest->onTaskBreakRecord(task_id,0,record.fastest_break_times);

		record.slowest_char_id = char_id0;
		if(!char_name0.empty() && char_name0.length() < MAX_CHAR_NAME_SIZE)
			strcpy(record.slowest_char_name,char_name0.c_str());
		record.slowest_elapsed_time = elapsed_time;
		record.slowest_time = (Int32)time(NULL);
		record.slowest_break_times += 1;
		pPlayerDest->onTaskBreakRecord(task_id,1,record.slowest_break_times);

		record.times_char_id = char_id0;
		if(!char_name0.empty() && char_name0.length() < MAX_CHAR_NAME_SIZE)
			strcpy(record.times_char_name,char_name0.c_str());
		record.times_times = times;
		record.times_time = (Int32)time(NULL);
		record.times_break_times += 1;
		pPlayerDest->onTaskBreakRecord(task_id,2,record.times_break_times);
		sendTaskRecordUpdate(record);
	}
	else
	{
		bool bNeedUpdate = false;
		TaskRecord& record = m_mapTaskRecord[task_id];
		if (record.fastest_elapsed_time == 0 || elapsed_time < record.fastest_elapsed_time)
		{
			record.fastest_char_id = char_id0;
			if(!char_name0.empty() && char_name0.length() < MAX_CHAR_NAME_SIZE)
				strcpy(record.fastest_char_name,char_name0.c_str());
			record.fastest_elapsed_time = elapsed_time;
			record.fastest_time = (Int32)time(NULL);
			record.fastest_break_times += 1;
			pPlayerDest->onTaskBreakRecord(task_id,0,record.fastest_break_times);
			bNeedUpdate = true;

		}
		if (elapsed_time > record.slowest_elapsed_time)
		{
			record.slowest_char_id = char_id0;
			if(!char_name0.empty() && char_name0.length() < MAX_CHAR_NAME_SIZE)
				strcpy(record.slowest_char_name,char_name0.c_str());
			record.slowest_elapsed_time = elapsed_time;
			record.slowest_time = (Int32)time(NULL);
			record.slowest_break_times += 1;
			pPlayerDest->onTaskBreakRecord(task_id,1,record.slowest_break_times);
			bNeedUpdate = true;
		}
		if (times > record.times_times)
		{
			record.times_char_id = char_id0;
			if(!char_name0.empty() && char_name0.length() < MAX_CHAR_NAME_SIZE)
				strcpy(record.times_char_name,char_name0.c_str());
			record.times_times = times;
			record.times_time = (Int32)time(NULL);
			record.times_break_times += 1;
			pPlayerDest->onTaskBreakRecord(task_id,2,record.times_break_times);
			bNeedUpdate = true;
		}
		if (bNeedUpdate)
		{
			sendTaskRecordUpdate(record);
		}
	}
}
void TaskManager::sendTaskRecordUpdate(TaskRecord& record)
{
	PacketEncoder encoder;
	encoder.AddItem(Int32(0));
	encoder.AddItem(Int32(1102));
	encoder.AddItem(Int32(record.task_id));
	DataBlock blkInfo;
	blkInfo.AppendData(&record,sizeof(record));
	encoder.AddItem(blkInfo);
	SendPacketToData(encoder);
}